package cn.dnui_zzh807.dnuifood_16110100807.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

import cn.dnui_zzh807.dnuifood_16110100807.R;
import cn.dnui_zzh807.dnuifood_16110100807.SomethingElse.UserPurchase;
import cn.dnui_zzh807.dnuifood_16110100807.SomethingElse.User_Comments;
import cn.dnui_zzh807.dnuifood_16110100807.SomethingElse.myDetails;
import cn.dnui_zzh807.dnuifood_16110100807.SomethingElse.myTwoDCode;
import cn.dnui_zzh807.dnuifood_16110100807.SomethingElse.userUpdate;
import cn.dnui_zzh807.dnuifood_16110100807.bean.UserInfo;
import cn.dnui_zzh807.dnuifood_16110100807.listener.OnRetrofitListener;
import cn.dnui_zzh807.dnuifood_16110100807.model.UserModel;

/**
 * Created by Dobby on 2018/11/5.
 */
public class Fragment4 extends Fragment implements OnRetrofitListener<UserInfo> {
    private ImageView blurImageView,avatarImageView;
    private RecyclerView recyclerView;
    private TextView username, userpass,phone,address,comment;
    private View view;
    private ImageView userpic,scan,user_details;
    private UserInfo userInfo;
    private LinearLayout dingdan,myComment,uerUpdata,myTwoCode,setting,user;
    private myDetails mydetails;
    private boolean isGetData = false;
    private String user_id;

    public Fragment4() {

    }
    @Override
    public void onViewCreated(@NonNull View views, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(views, savedInstanceState);
        this.view=views;
        SharedPreferences sharedPreferences = views.getContext().getSharedPreferences("zzh", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getString("user_id","user_id");
        starview();
        initData(user_id);
        click();
    }


    void initData(String user_id){
        UserModel userModel =new UserModel();
        userModel.getUserById(user_id,this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_fragment4, container, false);
    }

    @Override
    public void onSuccess(UserInfo userInfos, int flag) {
        this.userInfo=userInfos;
        startAdapter();
    }

    @Override
    public void onFail() {
        Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_SHORT).show();
    }

    private void starview(){
        username=view.findViewById(R.id.h_name);
        phone=view.findViewById(R.id.h_tel);
        address=view.findViewById(R.id.h_address);
        userpic=view.findViewById(R.id.h_head_pic);
        user_details=view.findViewById(R.id.user_details);
        dingdan=view.findViewById(R.id.dingdan);
        myComment=view.findViewById(R.id.comment1);
        uerUpdata=view.findViewById(R.id.userupdata);
        myTwoCode=view.findViewById(R.id.mytwoDcode);
        setting=view.findViewById(R.id.setting);
        user=view.findViewById(R.id.user);
    }
    private void startAdapter(){
        username.setText(userInfo.getUsername());
        phone.setText(userInfo.getMobilenum());
        address.setText(userInfo.getAddress());
//        Picasso.get().load(Constants.BASE_URL + userInfo.getUserpic()).into(userpic);
//        if () {
//               userpic.setImageResource(R.drawable.i2);
//        }
    }
    private void click(){
        myTwoCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),myTwoDCode.class);
                startActivity(intent);
            }
        });

        user_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1 = String.valueOf(userInfo.getUsername());
                String str2 = String.valueOf(userInfo.getUserpass());
                String str3 = String.valueOf(userInfo.getMobilenum());
                String str4 = String.valueOf(userInfo.getAddress());
                String str5 = String.valueOf(userInfo.getComment());
                //mydetails= new myDetails(userInfo.getUsername(),userInfo.getUserpass(),userInfo.getMobilenum(),userInfo.getAddress(),userInfo.getComment());
                Intent intent =new Intent(getActivity(),myDetails.class);
                intent.putExtra("username",str1);
                intent.putExtra("userpass",str2);
                intent.putExtra("phone",str3);
                intent.putExtra("address",str4);
                intent.putExtra("comment",str5);
                startActivity(intent);
            }
        });

        uerUpdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1 = String.valueOf(userInfo.getUsername());
                String str2 = String.valueOf(userInfo.getUserpass());
                String str3 = String.valueOf(userInfo.getMobilenum());
                String str4 = String.valueOf(userInfo.getAddress());
                String str5 = String.valueOf(userInfo.getComment());
                Intent intent =new Intent(getActivity(),userUpdate.class);
                intent.putExtra("username",str1);
                intent.putExtra("userpass",str2);
                intent.putExtra("phone",str3);
                intent.putExtra("address",str4);
                intent.putExtra("comment",str5);
                startActivity(intent);
            }
        });

        dingdan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=user_id;
                Intent intent = new Intent(getActivity(),UserPurchase.class);
                intent.putExtra("user_id",str);
                startActivity(intent);
            }
        });

        myComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=user_id;
                Intent intent = new Intent(getActivity(),User_Comments.class);
                intent.putExtra("user_id",str);
                startActivity(intent);
            }
        });
    }

    public void openCamera(){
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File appDir = new File(Environment.getExternalStorageDirectory()+"/zhongzhenhui");
        if (!appDir.exists()){
            appDir.mkdir();
        }
        Uri mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/zhongzhenhui/","zhongzhenhui" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,mUri);
        startActivityForResult(cameraIntent,1);
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        //   进入当前Fragment
        if (enter && !isGetData) {
            isGetData = true;
            //   这里可以做网络请求或者需要的数据刷新操作
//            GetData();
        } else {
            isGetData = false;
        }
        return super.onCreateAnimation(transit, enter, nextAnim);
    }
    @Override
    public void onPause() {
        super.onPause();
        isGetData = false;
    }

}